package com.yuju.order.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.account.enums.SettleAccountReturnCodeEnum;
import com.yuju.account.service.IAccountService;
import com.yuju.enums.StatusEnum;
import com.yuju.exception.BusinessException;
import com.yuju.hotel.model.RoomModel;
import com.yuju.hotel.service.IRoomService;
import com.yuju.model.account.AccountModel;
import com.yuju.order.enums.OrderAccountReturnCodeEnum;
import com.yuju.order.enums.ReserveReturnCodeEnum;
import com.yuju.order.enums.ReserveStatusEnum;
import com.yuju.order.model.ReserveModel;
import com.yuju.order.service.IOrderAccountService;
import com.yuju.order.service.IReserverService;
import com.yuju.order.service.IRoomOrderService;
import com.yuju.util.Utils;

/**
 * 
 * 类名：OrderAccountServiceImpl  <br />
 *
 * 功能：订单账务 1.nowshow 2.走结 3.退房
 *
 * @author : xuehj <br />
 * @Date : 2017年12月14日 上午11:47:35  <br />
 * @version : 1.1 <br />
 */
@Service
public class OrderAccountServiceImpl implements IOrderAccountService {
	
	@Autowired
	private IRoomOrderService roomOrderService;

	@Autowired
	private IAccountService accountService;
	
	@Autowired
	private IRoomService roomService;
	@Autowired
	private IReserverService reserverService;
	
	/**
	 * 
	 * 功能: NOWSHOW房费<br/>
	 * 
	 * 重写：xuehj <br/>
	 * 
	 * @Date ：2017年12月14日 上午11:48:46<br/>
	 * 房费<br/>
	 *
	 * @param accounts 账务
	 * @return 成功是返回大于0的数值，反之为小于0的数值<br/>
	 * @see com.yuju.order.service.IOrderAccountService#nowShow(java.util.List)
	 */
	@Override
	public int saveNoShow(List<AccountModel> accounts,String empId) {
		for(AccountModel accountModel : accounts){
			if(accountModel.getReserveId() != null){
				ReserveModel reserveModel = new ReserveModel();
				reserveModel.setId(accountModel.getReserveId());
				reserveModel.setGmtModified(new Date());
				reserveModel.setReserveStatus(ReserveStatusEnum.NOSHOW.getValue());
				reserveModel.setReserveStatusName(ReserveStatusEnum.NOSHOW.getName());
				if(reserverService.updateSelective(reserveModel)==0){
					throw new BusinessException
					(ReserveReturnCodeEnum.RESERVE_UPDATE_FAILURE.getCode(),
							ReserveReturnCodeEnum.RESERVE_UPDATE_FAILURE.getDesc());
				}
			}
		}
		return accountService.saveNoShow(accounts);
	}

	/**
	 * 
	 * 功能: 更新订单状态、房间状态<br/>
	 * 1.更新订单状态为走结
	 * 2.更新房间为脏房
	 * 
	 * 重写：xuehj <br/>
	 * 
	 * @Date ：2017年12月14日 上午11:52:33<br/>
	 * 
	 * @param roomOrderId 房间订单ID
	 * @param goReason 走结原因
	 * @param empId 员工ID
	 * @param roomId 房间ID
	 * @return 成功是返回大于0的数值，反之为小于0的数值<br/>
	 * @see com.yuju.order.service.IOrderAccountService#goSettleAccount(com.yuju.model.account.AccountModel)
	 */
	@Override
	public int saveSettleAccount(String roomOrderId, String goReason, String empId, String roomId) {
		// 1.更新订单状态为走结
		int rows = roomOrderService.updateRoomOrderForZJ(roomOrderId, goReason, empId);
		if (rows == 0) {
			throw new BusinessException(SettleAccountReturnCodeEnum.SETTLEACCOUNT_ZJ_SUCCESS.getCode(), 
					SettleAccountReturnCodeEnum.SETTLEACCOUNT_ZJ_SUCCESS.getDesc());
		}
		// 2.更新房间为脏房
		RoomModel roomModel = new RoomModel();
		roomModel.setId(roomId);
		roomModel.setIsDirty(StatusEnum.TRUE.getValue());
		rows = roomService.updateSelective(roomModel);
		return rows;
	}

	/**
	 * 
	 * 功能: 退房<br/>
	 * 1.更新账务
	 * 2.更新结账状态
	 * 3.更新房间为脏房
	 * 
	 * 重写：xuehj <br/>
	 * 
	 * @Date ：2017年12月14日 上午11:52:04<br/>
	 * 
	 * @param accountModel 账务数据
	 * @param roomId 房间ID
	 * @return 成功是返回大于0的数值，反之为小于0的数值<br/>
	 * @see com.yuju.order.service.IOrderAccountService#checkOut(com.yuju.model.account.AccountModel)
	 */
	@Override
	public int saveCheckOut(List<AccountModel> list, String roomId,String roomOrderId, String empId) {
		int rows = 0;
		if(Utils.isNotNull(list) && list.size() != 0){
			// 1.更新账务
			rows = accountService.saveEndSettle(list,empId);
			if (rows == 0) {
				throw new BusinessException(OrderAccountReturnCodeEnum.ORDER_ACCOUNT_CHECKOUT_FAILURE.getCode(), 
						OrderAccountReturnCodeEnum.ORDER_ACCOUNT_CHECKOUT_FAILURE.getDesc());
			}
		}
		// 2.更新结账状态
		rows = roomOrderService.updateRoomOrderForJS(roomOrderId, empId);
		if (rows == 0) {
			throw new BusinessException(OrderAccountReturnCodeEnum.ORDER_ACCOUNT_SETTLE_STATUS_FAILURE.getCode(), 
					OrderAccountReturnCodeEnum.ORDER_ACCOUNT_SETTLE_STATUS_FAILURE.getDesc());
		}
		// 3.更新房间为脏房
		RoomModel roomModel = new RoomModel();
		roomModel.setId(roomId);
		roomModel.setIsDirty(StatusEnum.TRUE.getValue());
		rows = roomService.updateSelective(roomModel);
		
		if (rows == 0) {
			throw new BusinessException(OrderAccountReturnCodeEnum.UPDATE_ROOM_DIRTY_STATUS_FAILURE.getCode(),
					OrderAccountReturnCodeEnum.UPDATE_ROOM_DIRTY_STATUS_FAILURE.getDesc());
		}
		return rows;
	}

}
